﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Security.Principal;

namespace OutlookCachedEnable
{
    class Program
    {
        static void Main(string[] args)
        {
            //ChangeKey(ActionEnum.Enabled, "testProfile", false);  

            if (args.Length != 2)
            {
                PrintUsage();
                return;
            }

            switch (args[1])
            {
                case "1":
                    {
                        ChangeKey(ActionEnum.Enabled,args[0], false); break;
                    }
                case "2":
                    {
                        ChangeKey(ActionEnum.EnabledWithPublicFolder, args[0], false); break;
                    }
                case "3":
                    {
                        ChangeKey(ActionEnum.Disabled, args[0], false); break;
                    }
                default:PrintUsage();break;
            }
        }
        /// <summary>
        /// NOTE: The output may differ depending on the endianess of your computer's architecture.
        /// </summary>
        /// <param name="AndSubkeys"></param>
        private static void ChangeKey(ActionEnum action, string ProfileName, bool AndSubkeys)
        {
            if (BitConverter.IsLittleEndian)
            {
               // Console.WriteLine("LittleEndian architecture detected, as expected.");
            }
            else
            {
                Console.WriteLine("BigEndian Architecture detected - Warning, not tested!");
            }
             

            //
            // Convert Action into new Binary value
            //

            byte[] Enabled = { 0x84, 0x01, 0x00, 0x00 };
            byte[] EnabledwithPublicFolder = { 0x84, 0x05, 0x00, 0x00 };
            byte[] Disabled = { 0x04, 0x00, 0x00, 0x00 };

            byte[] valueToSet = Enabled;
            switch (action)
            {
                case ActionEnum.Enabled:
                    valueToSet = Enabled;
                    break;
                case ActionEnum.EnabledWithPublicFolder:
                    valueToSet = EnabledwithPublicFolder;
                    break;
                case ActionEnum.Disabled:
                    valueToSet = Disabled;
                    break;
                default:
                    break;
            }

             

            //
            // Detect OS and set starting key
            //
            string TS2003 = @"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\"+ProfileName;
            var userSID = WindowsIdentity.GetCurrent();
            string sidString = userSID.User.ToString();
            string TS2008R2 = @"HKEY_USERS\"+ sidString+@"\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\" + ProfileName;

            //
            // Does profile exist?
            //
            var userKey = Registry.CurrentUser.OpenSubKey(TS2003);
            var usersKey = Registry.Users.OpenSubKey(TS2008R2);

            if (userKey == null) //todo: and OS isn't 2008R2
            {
                Console.WriteLine("The profile: " + ProfileName + " does not exist.  Quitting.");
                System.Environment.Exit(1);
            }


            string keyName = TS2003;

            //
            // Arrays of Byte are stored automatically as Binary. Per MSDN http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx
            //
            Registry.SetValue(keyName+ @"\13dbb0c8aa05101a9bb000aa002fc45a", "00036601", valueToSet);
            object ttt = Registry.GetValue(keyName, "00036601", null);

             
            if (AndSubkeys == true)
            {
                // Iterate through registry and set the value accordingly.
            }
        }

        private static void PrintUsage()
        {
            Console.WriteLine("OutlookCachedEnable.exe Enable Cached Mode for Outlook 2003/2007 on terminal server");
            Console.WriteLine("This is an unsupported registry modification. No Warranty implied or included.");
            Console.WriteLine("");
            Console.WriteLine("This is not needed for Outlook 2010.  The feature is built in see:");
            Console.WriteLine("http://technet.microsoft.com/en-us/library/ee620554(office.14).aspx");
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Syntax: ");
            Console.WriteLine("ProfileNameToEdit 1  = Enable  ");
            Console.WriteLine("ProfileNameToEdit 2  = Enable with Public Folder caching  ");
            Console.WriteLine("ProfileNameToEdit 3  = Disable  ");
            Console.WriteLine("");
            Console.WriteLine("Example: ");
            Console.WriteLine("The following command edits the outlook profile 'Outlook' and");
            Console.WriteLine("enables the OST for this user");
            Console.WriteLine("OutlookCacheEnable Outlook 1");
            return;
        }
    }
    public enum ActionEnum
    {
        Enabled,
        EnabledWithPublicFolder,
        Disabled
    }
   
}
